﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Scavenger.VectorStuff
{
    class Line
    {
        private Vector2 p_PointStart;
        private Vector2 p_PointEnd;
        private Vector2 p_Normal;
        private Vector2 p_DirectionVector;
        private float DValue, Length;
        public Vector2 PointStart { get { return p_PointStart; } }
        public Vector2 PointEnd { get { return p_PointEnd; } }
        public Vector2 Normal { get { return p_Normal; } }
        public Vector2 DirectionVector { get { return p_DirectionVector; } }
        public float Constant { get { return DValue; } }

        public Line(Vector2 pointStart, Vector2 pointEnd)
        {
            p_PointStart = pointStart;
            p_PointEnd = pointEnd;
            p_DirectionVector = VectorMath.PointsToVector(pointStart, pointEnd);
            Length = p_DirectionVector.Length();
            p_DirectionVector.Normalize();
            p_Normal = VectorMath.LineNormal(p_DirectionVector);
            DValue = VectorMath.DotProduct(p_PointStart, p_Normal);
        }

        public void Update(Vector2 pointOrNormal, String vectorType)
        {
            if (vectorType == "Start")
            {
                p_PointStart = pointOrNormal;
                p_DirectionVector = VectorMath.PointsToVector(pointOrNormal, p_PointEnd);
                Length = p_DirectionVector.Length();
                p_DirectionVector.Normalize();
                p_Normal = VectorMath.LineNormal(p_DirectionVector);
            }

            else if (vectorType == "End")
            {
                p_PointEnd = pointOrNormal;
                p_DirectionVector = VectorMath.PointsToVector(p_PointStart, pointOrNormal);
                Length = p_DirectionVector.Length();
                p_DirectionVector.Normalize();
                p_Normal = VectorMath.LineNormal(p_DirectionVector);
            }

            else if (vectorType == "Normal")
            {
                p_Normal = pointOrNormal;
                p_Normal.Normalize();
                p_DirectionVector = VectorMath.LineNormal(- p_Normal);
                p_PointEnd = p_PointStart + Length * p_DirectionVector;
            }
            else if (vectorType == "Direction")
            {
                p_DirectionVector = pointOrNormal;
                p_DirectionVector.Normalize();
                p_Normal = VectorMath.LineNormal(p_DirectionVector);
                p_PointEnd = p_PointStart + Length * p_DirectionVector;

            }
            DValue = VectorMath.DotProduct(p_PointStart, p_Normal);
        }

        public float DistanceToLine(Vector2 point)
        {
            return VectorMath.DotProduct(point, p_Normal) - DValue;
        }

        public Vector2 ClosestPointOnLine(Vector2 point)
        {
            float distance = DistanceToLine(point);
            Vector2 PointOnLine = point - Normal * distance;
            return PointOnLine;
        }

    }
}
